/*
 * Date：10/23/18 11:18 PM
 * Author: guomw
 *
 */

package org.elanfox.intellicommunity.intellicommunityadmin.base;

import lombok.AllArgsConstructor;
import org.elanfox.intellicommunity.biz.service.core.auth.SystemService;
import org.elanfox.intellicommunity.intellicommunityadmin.annotation.OperatorId;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;

/**
 * /**
 *
 * @author guomw
 * @date 2018/9/21
 */
@AllArgsConstructor
public class OperatorIdArgumentResolver implements HandlerMethodArgumentResolver {
    private SystemService systemService;

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.getParameterAnnotation(OperatorId.class) != null && methodParameter.getParameterType().isAssignableFrom(Integer.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        return systemService.getCurrentRootId(nativeWebRequest.getNativeRequest(HttpServletRequest.class));
    }
}

